import React, {
  createContext, useContext, useEffect,
  // useState
} from 'react'
import { w3cwebsocket as W3CWebSocket } from 'websocket'
// import { http } from '@/utils'
// import { useStore } from '@/store'

const SocketContext = createContext()

export const useSocket = () => useContext(SocketContext)

const SocketProvider = ({ children }) => {
  const socket = new W3CWebSocket('ws://192.168.8.60:30258/websocket')
  // const { loginStore } = useStore()
  // const [orderUserList, setOrderUserList] = useState([])

  useEffect(() => {
    socket.onopen = () => {
      console.log('WebSocket Client Connected')
    }
    socket.onmessage = (message) => {
      // console.log('Received:', message)
    }
    socket.onclose = () => {
      console.log('WebSocket Client Disconnected')
    }
    // 获取用户持仓
    // const getPosition = async () => {
    //   const res = await http.post('/alarm/deliversign/position', { userId: loginStore.userId })
    //   const list = []
    //   for (const key in res.body) {
    //     const element = res.body[key]
    //     const row = {
    //       _id: key,
    //       ...element
    //     }

    //     list.push(row)
    //   }
    //   // setOrderUserList(list)
    // }
    // getPosition()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  return (
    <SocketContext.Provider value={socket}>
      {children}
    </SocketContext.Provider>
  )
}

export default SocketProvider